package com.javasm.finance.controller;

import com.javasm.finance.entity.*;
import com.javasm.finance.service.ProdService2;
import com.javasm.finance.service.impl.ProdService2Impl;
import com.javasm.finance.util.RespUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/basic/*")
public class Prod2Controller extends BaseController{
    ProdService2 prodService2 =new ProdService2Impl();

    public void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageStr = req.getParameter("page");
        Integer page = 1;
        if (pageStr != null && !"".equals(pageStr)) {
            page = Integer.valueOf(pageStr);
        }
        String pageSizeStr = req.getParameter("pageSize");
        Integer pageSize = 5;
        if (pageSizeStr != null && !"".equals(pageSizeStr)) {
            pageSize = Integer.valueOf(pageSizeStr);
        }
        String prodStateStr = req.getParameter("prodState");
        Integer prodState = null;
        if (prodStateStr != null && !"".equals(prodStateStr)) {
            prodState = Integer.valueOf(prodStateStr);
        }
        String prodName = req.getParameter("prodName");
        String prodCategoryName = req.getParameter("prodCategoryName");
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        String managerName = user.getUserName();
        //调用方法
        List<Prod> prodList = prodService2.queryProd(page, pageSize, prodName, prodCategoryName,managerName,prodState);
        //设置响应数据
        ReturnEntity re = new ReturnEntity();
        if (prodList != null && prodList.size() > 0) {
            //查询数据总条数
            long totalNum = prodService2.findTotalNum(prodName,prodCategoryName,managerName,prodState);
            PageInfo pageInfo = new PageInfo(page, pageSize, totalNum);
            //创建Map集合
            Map<String, Object> returnMap = new HashMap<>();
            returnMap.put("prodList", prodList);
            returnMap.put("pageInfo", pageInfo);
            re.setReturnCode(CodeAndMsg.QUERY_SUCCESS.getReturnCode());
            re.setReturnMsg(CodeAndMsg.QUERY_SUCCESS.getReturnMsg());
            re.setReturnData(returnMap);
        } else {
            //创建Map集合
            Map<String, Object> returnMap = new HashMap<>();
            returnMap.put("pageInfo", new PageInfo(1, 5, 0l));
            re.setReturnCode(CodeAndMsg.QUERY_FAILED.getReturnCode());
            re.setReturnMsg(CodeAndMsg.QUERY_FAILED.getReturnMsg());
            re.setReturnData(returnMap);
        }
        //写出响应数据
        RespUtils.handleResp(resp, re);
    }

    public void querySeries(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        List<ProductCategory> list = prodService2.querySeries();
        //设置响应数据
        ReturnEntity re = new ReturnEntity();
        if (list != null && list.size() > 0) {
            re.setReturnCode(CodeAndMsg.QUERY_SUCCESS.getReturnCode());
            re.setReturnMsg(CodeAndMsg.QUERY_SUCCESS.getReturnMsg());
            re.setReturnData(list);
        } else {
            re.setReturnCode(CodeAndMsg.QUERY_FAILED.getReturnCode());
            re.setReturnMsg(CodeAndMsg.QUERY_FAILED.getReturnMsg());
        }
        //写出响应数据
        RespUtils.handleResp(resp, re);
    }

    public void queryManager(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        String userName = user.getUserName();
        List<User> list = prodService2.queryManager(userName);
        //设置响应数据
        ReturnEntity re = new ReturnEntity();
        if (list != null && list.size() > 0) {
            re.setReturnCode(CodeAndMsg.QUERY_SUCCESS.getReturnCode());
            re.setReturnMsg(CodeAndMsg.QUERY_SUCCESS.getReturnMsg());
            re.setReturnData(list);
        } else {
            re.setReturnCode(CodeAndMsg.QUERY_FAILED.getReturnCode());
            re.setReturnMsg(CodeAndMsg.QUERY_FAILED.getReturnMsg());
        }
        //写出响应数据
        RespUtils.handleResp(resp, re);
    }

    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String prodName = req.getParameter("prodName");
        String prodCategoryName = req.getParameter("prodCategoryName");
        String parentClass = req.getParameter("parentClass");
        String prodChannelName = req.getParameter("prodChannelName");
        String opeTime = req.getParameter("opeTime");
        String currencyType = req.getParameter("currencyType");
        String anlRate = req.getParameter("anlRate");
        String subsPeriod = req.getParameter("subsPeriod");
        String fundManageRate = req.getParameter("fundManageRate");
        String subsRate = req.getParameter("subsRate");
        String iniInvestAmt = req.getParameter("iniInvestAmt");
        String subsCoMethod = req.getParameter("subsCoMethod");
        String redCycle = req.getParameter("redCycle");
        String redStartAmt = req.getParameter("redStartAmt");
        String redFee = req.getParameter("redFee");
        String lockTime = req.getParameter("lockTime");
        String managerName = req.getParameter("managerName");
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        String creatorName = user.getUserName();
        Prod prod=new Prod(prodName,prodCategoryName,parentClass,prodChannelName,opeTime,currencyType,anlRate,subsPeriod,fundManageRate,subsRate,iniInvestAmt,subsCoMethod,redCycle,redStartAmt,redFee,lockTime,managerName,creatorName);

        int rows = prodService2.addProd(prod);
        //设置响应数据
        ReturnEntity re = new ReturnEntity();
        if (rows > 0) {
            re.setReturnCode(CodeAndMsg.OPERATE_SUCCESS.getReturnCode());
            re.setReturnMsg(CodeAndMsg.OPERATE_SUCCESS.getReturnMsg());
        } else {
            re.setReturnCode(CodeAndMsg.OPERATE_FAILED.getReturnCode());
            re.setReturnMsg(CodeAndMsg.OPERATE_FAILED.getReturnMsg());
        }
        //写出响应数据
        RespUtils.handleResp(resp, re);
    }

    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        //接收请求数据
        String prodIdStr = req.getParameter("prodId");
        Integer prodId = null;
        if (prodIdStr != null && !"".equals(prodIdStr)) {
            prodId = Integer.valueOf(prodIdStr);
        }
        //调用方法
        int rows = prodService2.deleteProd(prodId);
        ReturnEntity re = new ReturnEntity();
        if (rows > 0) {
            re.setReturnCode(CodeAndMsg.OPERATE_SUCCESS.getReturnCode());
            re.setReturnMsg(CodeAndMsg.OPERATE_SUCCESS.getReturnMsg());
        } else {
            re.setReturnCode(CodeAndMsg.OPERATE_FAILED.getReturnCode());
            re.setReturnMsg(CodeAndMsg.OPERATE_FAILED.getReturnMsg());
        }
        //写出响应数据
        RespUtils.handleResp(resp, re);
    }

    public void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{

        String prodIdStr = req.getParameter("prodId");
        Integer prodId = null;
        if (prodIdStr != null && !"".equals(prodIdStr)) {
            prodId = Integer.valueOf(prodIdStr);
        }

        Prod byPid = prodService2.findByPid(prodId);
        //设置响应数据
        ReturnEntity re = new ReturnEntity();
        if (byPid != null) {
            re.setReturnCode(CodeAndMsg.QUERY_SUCCESS.getReturnCode());
            re.setReturnMsg(CodeAndMsg.QUERY_SUCCESS.getReturnMsg());
            re.setReturnData(byPid);
        } else {
            re.setReturnCode(CodeAndMsg.QUERY_FAILED.getReturnCode());
            re.setReturnMsg(CodeAndMsg.QUERY_FAILED.getReturnMsg());
        }
        //写出响应数据
        RespUtils.handleResp(resp, re);
    }

    public void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String prodIdStr = req.getParameter("prodId");
        Integer prodId = null;
        if (prodIdStr != null && !"".equals(prodIdStr)) {
            prodId = Integer.valueOf(prodIdStr);
        }
        String prodName = req.getParameter("prodName");
        String prodCategoryName = req.getParameter("prodCategoryName");
        String parentClass = req.getParameter("parentClass");
        String prodChannelName = req.getParameter("prodChannelName");
        String opeTime = req.getParameter("opeTime");
        String currencyType = req.getParameter("currencyType");
        String anlRate = req.getParameter("anlRate");
        String subsPeriod = req.getParameter("subsPeriod");
        String fundManageRate = req.getParameter("fundManageRate");
        String subsRate = req.getParameter("subsRate");
        String iniInvestAmt = req.getParameter("iniInvestAmt");
        String subsCoMethod = req.getParameter("subsCoMethod");
        String redCycle = req.getParameter("redCycle");
        String redStartAmt = req.getParameter("redStartAmt");
        String redFee = req.getParameter("redFee");
        String lockTime = req.getParameter("lockTime");
        String managerName = req.getParameter("managerName");
        Prod prod=new Prod(prodId,prodName,prodCategoryName,parentClass,prodChannelName,opeTime,currencyType,anlRate,subsPeriod,fundManageRate,subsRate,iniInvestAmt,subsCoMethod,redCycle,redStartAmt,redFee,lockTime,managerName);
        int rows = prodService2.editProd(prod);
        //设置响应数据
        ReturnEntity re = new ReturnEntity();
        if (rows > 0) {
            re.setReturnCode(CodeAndMsg.OPERATE_SUCCESS.getReturnCode());
            re.setReturnMsg(CodeAndMsg.OPERATE_SUCCESS.getReturnMsg());
        } else {
            re.setReturnCode(CodeAndMsg.OPERATE_FAILED.getReturnCode());
            re.setReturnMsg(CodeAndMsg.OPERATE_FAILED.getReturnMsg());
        }
        //写出响应数据
        RespUtils.handleResp(resp, re);
    }

    public void toRemi(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{

        String prodIdStr = req.getParameter("prodId");
        Integer prodId = null;
        if (prodIdStr != null && !"".equals(prodIdStr)) {
            prodId = Integer.valueOf(prodIdStr);
        }

        Prod byPid = prodService2.findByPidRemi(prodId);
        //设置响应数据
        ReturnEntity re = new ReturnEntity();
        if (byPid != null) {
            re.setReturnCode(CodeAndMsg.QUERY_SUCCESS.getReturnCode());
            re.setReturnMsg(CodeAndMsg.QUERY_SUCCESS.getReturnMsg());
            re.setReturnData(byPid);
        } else {
            re.setReturnCode(CodeAndMsg.QUERY_FAILED.getReturnCode());
            re.setReturnMsg(CodeAndMsg.QUERY_FAILED.getReturnMsg());
        }
        //写出响应数据
        RespUtils.handleResp(resp, re);
    }

    public void remi(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String prodIdStr = req.getParameter("prodId");
        Integer prodId = null;
        if (prodIdStr != null && !"".equals(prodIdStr)) {
            prodId = Integer.valueOf(prodIdStr);
        }
        String averNav = req.getParameter("averNav");
        String netValBaseDate = req.getParameter("netValBaseDate");
        String culGrowRate = req.getParameter("culGrowRate");
        Prod prod=new Prod(prodId,averNav,netValBaseDate,culGrowRate);
        int rows = prodService2.addRemi(prod);
        //设置响应数据
        ReturnEntity re = new ReturnEntity();
        if (rows > 0) {
            re.setReturnCode(CodeAndMsg.OPERATE_SUCCESS.getReturnCode());
            re.setReturnMsg(CodeAndMsg.OPERATE_SUCCESS.getReturnMsg());
        } else {
            re.setReturnCode(CodeAndMsg.OPERATE_FAILED.getReturnCode());
            re.setReturnMsg(CodeAndMsg.OPERATE_FAILED.getReturnMsg());
        }
        //写出响应数据
        RespUtils.handleResp(resp, re);
    }
}
